Goto
Goto <.label.>
 
Parameters: NONE
Returns: NONE
 

      Goto is an unconditional change of control in your program. Meaning that Goto is used to jump from the current position in your program to another. The position of where we jump to is pinpointed by a type of marker, known as a Label .

      The label is a book mark in our program code. By placing a label at the start of a line. We can then jump directly to that section of our program, using the Goto statement.

      It's here we've got to be careful though, we don't want to develop a dependency upon them. As it's very easy to liter our programs with too many labels and goto / gosub statements. Which can rapidly turn some programming fun into a nightmare as our programs grow in size. The trouble occurs when our programs codew ends up jumping all over the place, rather than smoothly flowing through. This erratic jumping makes the program harder to follow, and ultimately very hard to track down problem areas in our programs.


      Important: While Labels can be placed virtually anywhere through a program, there are some limiations that PlayBASIC enforces.

      Which are,

      1) Labels must not be indented !

      2) Goto / Gosub can only jump to labels within the same scope.

      These have been put in place to help prevent some unsafe programming practices.





FACTS:


      * There's lots of Myths about using Goto to improve speed. Some of which are debunked bellow.

      * Goto expects the labels you give it, to be within the same scope. Meaning if you're inside a function, you can't jump outside of the function/psub and vice versa.

     * Unlike Gosub you can't return from a code section called via return.

      * When using Goto, make sure the Label you are calling is NOT indented. Also See ProgramLayout

      * Goto is fall back command from old school editions of basic. Today it's recommended that you use Functions or Psubs to create reusable sections of code. See Functions&Psub for a tutorial.

      * Labels are declared with a name followed by a : character and shouldn't be indented (See ProgramLayout)





Mini Tutorial:


      The differences between Gosub and Goto

  
; Jump to the code starting at Label2.
;
  Goto Label2
  
; labels are declared with a name followed by a :
Label1:
  Print "Hello this is the code of Label1"
  Print "... and good-bye"
; Return the statement after the GOSUB call
  Return
  
; If you follow through the programs logic, this line will never
; get executed.
  Print "This code will NEVER be executed"
  
; labels are declared with a name followed by a :
Label2:
  Print "This is the beginning."
  Print "Now we use Gosub Label1..."
  Gosub Label1
  Print "We returned from the code section 'Label1'"
  Print "And this ends our program"
  
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  


     This example would output.

  
  This is the beginning.
  Now we use Gosub Label1...
  Hello this is the code of Label1
  ... And good-bye
  We returned from the code section 'Label1'
  And this ends our program
  







Some Myths About Goto:



      If you're browsed around some programming forums, then you've probably noticed that the Goto is one of those statements that insights a very passionate divide between programmers. Personally, it's like any other statement at our disposable, if we over use it, or use it how wasn't intended, then we'll no doubt end up writing some pretty ugly and hard to read program code. However, this is an old debate, and one where some of the pro / con augments put forward/against Goto are no longer valid. So here we'll look a few popular myths concerning it.



GOTO is faster than DO/LOOPS, EXITS & CONTINUE


      False - This is one of the very persistent myths that's passed through programming communities. While it might well have been true back in the day and perhaps still is relevant in some editions of BASIC, it's not in PlayBASIC. So attempting these types of micro code optimizations is useless.

      Why ? - Do / Loops, Exits and Continues resolve into the same code as GOTO does.

Making this,


  
  Do
     X=X+1
     If X>100 Then Exit
  Loop
  


and this, are functionality identical.


  
StartOfLoop:
  X=X+1
  If X>100 Then Goto EndOFLoop
  Goto StartOfLoop
EndOFLoop:
  




Using GOTO to jump out of a loop (For - next, Repeat-Until, While-EndWhile) breaks the programs stack.



      False - This is another fairly common belief that does originally come from a place of truth, it's just that truth is some what outdated today. It's true that many old editions of BASIC, would use the stack to store information about the current looping structure. Meaning, in those editions of BASIC, jumping out of a loop via GOTO would cause our program to break. However, this is not true in Modern implementations of BASIC, since the stack isn't used for looping. So jumping out of a Loop structure, won't cause us any stack overflow problems.

      You can test this yourself, try running the following in DEBUG mode (f7), when the debuger appears then click upon the STATE tab. This will give us a overview of what resources this program is using. Towards the bottom of the overview information list, is a Stack Size: setting. Which will read zero.


  
  
  Do
     
     Cls
   ; STart of For/Next Loop
     For lp=0 To 100
        
      ; Jump out of this loop
        Goto JumpOutOfLoop
        
      ; the next statement never gets executed
      ; in this example. As the goto skips us out and
      ; over it
     Next
     
JumpOutOfLoop:
   ; Display some messages
     Print "Program Running"
     Print "loop Counter"+Str$(lp)
     
     Sync
     
  Loop
  
  
  




      While you can jump out of a loop using goto statement, you're really emulating the functionality of the EXIT, EXITFOR / CONTINUE statements. Therefore we highly recommend using those. Not only are they less typing (it's good to be lazy), they help us write cleaner code in the long run.


      The following example, is the same just that's using ExIT statement to jump out of the middle of the For-Next loop.

  
  
  Do
     
     Cls
   ; STart of For/Next Loop
     For lp=0 To 100
        
      ; Jump out of this loop
        Exit
        
      ; the next statement never gets executed
      ; in this example. As the goto skips us out and
      ; over it
     Next
     
   ; Display some messages
     Print "Program Running"
     Print "loop Counter"+Str$(lp)
     
     Sync
     
  Loop
  
  
  





 
Related Info: Continue | Exit | Function | Gosub | ProgramLayout | Psub | Return :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com